package com.yeu.action;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yeu.model.Event;
import com.yeu.util.ConnectionPool;
import com.yeu.util.ExcelUtil;

public class EventAction extends ActionSupport implements ModelDriven<Event>,SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Event event=new Event();
	private Map<String,Object> session;
	private Map<String, Boolean> boolResult;
	private Map<String,Object> mapResult;
	private List<Map<String,String>> result;
	private int rows;
	private int page;
	private String sort;
	private String order;
	
	public List<Map<String, String>> getResult() {
		return result;
	}

	public void setEvent(Event event) {
		this.event = event;
	}
	
	public Map<String, Boolean> getBoolResult() {
		return boolResult;
	}
	
	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	
	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	@Override
	public Event getModel() {
		return this.event;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session=session;
	}
	
	public String add(){
		event.setEventStatus("未完成");
		String sql="insert into EVENT (EVENTID,EVENTTYPE,EVENTDATE,EVENTSTATUS,REPORTERID,REPORTERNAME,EVENTADMINBY,REPORTERCLASS,REPORTERDEPARTMENT,REPORTERMONITORNAME,REPORTERDIRECTORNAME,REPORTERPHONENUM,REPORTERMAIL,EVENTCONTENT) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		if(ConnectionPool.executeQuery("select EVENTID from EVENT where EVENTID=?", event.getEventId()).size() != 0){
			boolResult=new HashMap<String, Boolean>();
			boolResult.put("RESULT", false);
		}
		else{
			boolResult=ConnectionPool.executeUpdate(sql
				,event.getEventId()
				,event.getEventType()
				,event.getEventDate()
				,event.getEventStatus()
				,event.getReporterId()
				,event.getReporterName()
				,event.getEventAdminBy()
				,event.getReporterClass()
				,event.getReporterDepartment()
				,event.getReporterMonitorName()
				,event.getReporterDirectorName()
				,event.getReporterPhoneNum()
				,event.getReporterMail()
				,event.getEventContent());
		}
		return "boolResult";
	}
	
	public String status(){
		String sql="update EVENT set EVENTSTATUS=? where EVENTID=?";
		boolResult=ConnectionPool.executeUpdate(sql, event.getEventStatus(),event.getEventId());
		return "boolResult";
	}
	
	public String eventId(){
		String eventId=(String) session.get("eventId");
		String sql="select * from EVENT,DEPARTMENTLEADER,USERTABLE,CLASS where EVENTID=? and EVENT.EVENTADMINBY=DEPARTMENTLEADER.DEPARTMENTID and DEPARTMENTLEADER.USERID=USERTABLE.USERID and EVENT.REPORTERCLASS=CLASS.CLASSID";
		result=ConnectionPool.executeQuery(sql, eventId);
		for (Map<String, String> map : result) {
			map.remove("ROLEID");
			map.remove("PASSWORD");
			map.remove("USERID");
			map.remove("CLASSDIRECTORNAME");
			map.remove("CLASSMONITORNAME");
		}
		return SUCCESS;
	}
	
	public String userId(){
		mapResult=new HashMap<String, Object>();
		String userId=(String) session.get("userId");
		String sql=String.format("SELECT * FROM (SELECT A.*,ROWNUM RN FROM (SELECT * FROM EVENT,DEPARTMENTLEADER,CLASS WHERE EVENTADMINBY=? AND EVENT.EVENTADMINBY=DEPARTMENTLEADER.USERID AND EVENT.REPORTERCLASS=CLASS.CLASSID ORDER BY %s %s) A WHERE ROWNUM<=%d) WHERE RN>%d",sort,order,rows*page,rows*(page-1));
		//System.out.println("sort="+sort+",order="+order+","+"rows="+rows+",page="+page);
		result=ConnectionPool.executeQuery(sql,userId);
		for (Map<String, String> map : result) {
			map.remove("ROLEID");
			map.remove("REPORTERDEPARTMENT");
			map.remove("CLASSDEPARTMENTID");
			map.remove("DEPARTMENTID");
			map.remove("REPORTERCLASS");
			map.remove("EVENTADMINBY");
			map.remove("USERID");
			map.remove("RN");
			map.remove("CLASSID");
		}
		sql="SELECT COUNT(*) FROM EVENT WHERE EVENTADMINBY=?";
		String total=ConnectionPool.executeQuery(sql,userId).get(0).get("COUNT(*)");
		mapResult.put("total", total);
		mapResult.put("rows", result);
		
		return "mapResult";
	}
	
	public String search(){
		mapResult=new HashMap<String, Object>();
		String userId=(String) session.get("userId");
		String sql=String.format("SELECT * FROM (SELECT A.*,ROWNUM RN FROM (SELECT * FROM EVENT,CLASS,DEPARTMENTLEADER WHERE EVENT.EVENTADMINBY=? AND EVENT.EVENTADMINBY=DEPARTMENTLEADER.USERID AND EVENT.REPORTERCLASS=CLASS.CLASSID AND (EVENTID LIKE ! OR EVENTTYPE LIKE ! OR REPORTERNAME LIKE ! OR REPORTERID LIKE ! OR REPORTERPHONENUM LIKE ! OR REPORTERMAIL LIKE ! OR CLASSNAME LIKE ! OR REPORTERMONITORNAME LIKE ! OR REPORTERDIRECTORNAME LIKE ! OR EVENTSTATUS LIKE ! OR EVENTCONTENT LIKE !)  ORDER BY %s %s) A WHERE ROWNUM<=%d) WHERE RN>%d",sort,order,rows*page,rows*(page-1));
		sql=sql.replaceAll("[!]", "'%"+event.getKeyWord()+"%'");
		result=ConnectionPool.executeQuery(sql, userId);
		for (Map<String, String> map : result) {
			map.remove("ROLEID");
			map.remove("REPORTERDEPARTMENT");
			map.remove("CLASSDEPARTMENTID");
			map.remove("DEPARTMENTID");
			map.remove("REPORTERCLASS");
			map.remove("EVENTADMINBY");
			map.remove("USERID");
			map.remove("RN");
			map.remove("CLASSID");
		}
		sql="SELECT COUNT(*) FROM EVENT,CLASS,DEPARTMENTLEADER WHERE EVENT.EVENTADMINBY=? AND EVENT.EVENTADMINBY=DEPARTMENTLEADER.USERID AND EVENT.REPORTERCLASS=CLASS.CLASSID AND (EVENTID LIKE ! OR EVENTTYPE LIKE ! OR REPORTERNAME LIKE ! OR REPORTERID LIKE ! OR REPORTERPHONENUM LIKE ! OR REPORTERMAIL LIKE ! OR CLASSNAME LIKE ! OR REPORTERMONITORNAME LIKE ! OR REPORTERDIRECTORNAME LIKE ! OR EVENTSTATUS LIKE ! OR EVENTCONTENT LIKE !)";
		sql=sql.replaceAll("[!]", "'%"+event.getKeyWord()+"%'");
		String total=ConnectionPool.executeQuery(sql, userId).get(0).get("COUNT(*)");
		mapResult.put("total", total);
		mapResult.put("rows", result);
		return "mapResult";
	}
	
	public String print(){
		Map<String, String> map=new LinkedHashMap<String, String>();
		map.put("报案人姓名",event.getReporterName());
		map.put("报案人学号",event.getReporterId());
		map.put("报案人班级",event.getReporterClass());
		map.put("报案人手机",event.getReporterPhoneNum());
		map.put("报案人邮箱",event.getReporterMail());
		map.put("报案人班主任",event.getReporterMonitorName());
		map.put("报案人辅导员",event.getReporterDirectorName());
		map.put("事件号",event.getEventId());
		map.put("事件类型",event.getEventType());
		map.put("事件日期",event.getEventDate());
		map.put("事件主管",event.getEventAdminByName());
		map.put("事件内容",event.getEventContent());
		event.setFileStream(ExcelUtil.create(map));
		event.setFileFileName(event.getEventId()+".xls");
		event.setFileContentType("application/vnd.ms-excel");
		return INPUT;
	}
}
